package autoteam.ui.dialogs;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import autoteam.Local;
import autoteam.ui.tabs.AutoTeamTabs;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class CoachProfileEditor extends AutoTeamDialog
{
    private static final long serialVersionUID = 4208523450767634174L;

    private String correctPath;
    private int id;
    private DataAccessObject dao;
    private final JDialog thisDialog = this;
    
    public CoachProfileEditor( int id )
    {
        super( );
        this.id = id;
        dao = new DataAccessObject( );
        edit_pop_up( "coach" );
    }
    
    public void edit_pop_up( final String name )
    {
        DBRecord coachRec = dao.getRecord( "coaches", new ColumnData( "id", id ) );

        JPanel inside = new JPanel( );
        
        final JLabel path = new JLabel( "Select a personal picture" );

        JButton find = new JButton( "Find" );
        JButton submit = new JButton( "Submit" );
        
        
        
        find.addActionListener( new ActionListener( )
        {
            
            public void actionPerformed( ActionEvent e )
            {
                JFileChooser pic_chooser = new JFileChooser( );
                int option = pic_chooser.showOpenDialog( thisDialog );
                if ( option == JFileChooser.APPROVE_OPTION )
                {
                    path.setText( "Path: "
                                  + ((pic_chooser.getSelectedFile( ) != null) ? pic_chooser.getSelectedFile( )
                                                                                           .getName( ) : "nothing") );
                    try
                    {
                        String path = pic_chooser.getSelectedFile( ).getCanonicalPath( );
                        if ( checkPath( path ) )
                            correctPath = path;
                    }
                    catch ( IOException ex )
                    {
                        Logger.getLogger( AutoTeamTabs.class.getName( ) ).log( Level.SEVERE, null, ex );
                    }
                }
            }
        } );
        
        JLabel inst = new JLabel( "Change Coach's Biography" );
        final JTextArea changeBio = new JTextArea( 5, 20 );
        changeBio.setText( (String) coachRec.getFieldValue( "biography" ) );
        changeBio.setEditable( true );
        changeBio.setLineWrap( true );
        changeBio.setWrapStyleWord( true );
        
        submit.addActionListener( new ActionListener( )
        {
            public void actionPerformed( ActionEvent e )
            {
                if ( changeBio.getText( ) == null )
                {
                    JOptionPane.showMessageDialog( null, "Write a biography before submitting", Local.get( "Error" ),
                                                   JOptionPane.ERROR_MESSAGE, null );
                }
                else if ( correctPath == null )
                {
                    JOptionPane.showMessageDialog( null, "Select a picture before submitting", Local.get( "Error" ),
                                                   JOptionPane.ERROR_MESSAGE, null );
                }
                else
                {
                    ColumnData[] identifiers = { new ColumnData( "id", id ) };
                    ColumnData[] changes = 
                    { 
                         new ColumnData( "picturepath", correctPath ),
                         new ColumnData( "biography", changeBio.getText( ) ) 
                    };
                    dao.updateRecord( "coaches", identifiers, changes );
                    closeDialog( );
                }
            }
        } );
        
        inside.setLayout( new GridBagLayout( ) );
        GridBagConstraints constrain = new GridBagConstraints( );
        
        constrain.weightx = 1;
        constrain.gridx = 2;
        constrain.gridy = 0;
        inside.add( path, constrain );
        
        constrain.gridx = 1;
        constrain.gridy = 1;
        constrain.gridwidth = 2;
        inside.add( find, constrain );
        
        constrain.gridx = 2;
        constrain.gridy = 2;
        constrain.weightx = 1;
        inside.add( inst, constrain );
        
        constrain.gridx = 0;
        constrain.gridy = 3;
        constrain.gridwidth = 4;
        inside.add( changeBio, constrain );
        
        constrain.gridx = 1;
        constrain.gridy = 4;
        inside.add( submit, constrain );
        
        // Set up the localization
        String language;
        String country;
        language = new String( "en" );
        country = new String( "US" );
        Local.initialize( language, country );
        
        this.add( inside );
        this.setDialogSize( new Dimension (240, 240) );
        this.setVisible( true );
    }
    
    protected void closeDialog() {
		this.setVisible(false);
		this.dispose();		
	}

	private boolean checkPath( String path )
    {
        File picture = new File( path );
        // this conditional's else statement is dead code because
        // the File object "picture" is initialized right above it
        // and will therefore not be null under any circumstances
        if ( picture != null )
        {
            return true;
        }
        else
        {
            System.err.println( "Couldn't find file: " + path );
            return false;
        }
    }

    @ Override
    protected void setButtonActions( AutoTeamDialog thisDialog )
    {
        // TODO Auto-generated method stub
        
    }
}
